<?php
/**
 * 作用：统计分析
 * 作者: shijy@soe-soe.com
 * 日期: 2016/5/12 11:03
 * 公司: 浙江宣逸网络科技有限公司<www.soe-soe.com>
 */
namespace Admin\Controller;
use Admin\BaseController;
use Admin\Model\AnalysisModel;
use Think\Controller;
use Think\Page;

class AnalysisController extends BaseController{

	/**
	 * 积分统计-积分产出
	 * Author:wwl
	 */
	public function pointOutPut(){
		$startDate = I('get.startDate','');
		$endDate = I('get.endDate','');
		$where = "1";

		if($startDate){
			$startTime = strtotime($startDate);
			$startTime = mktime(0,0,0,date("m",$startTime),date("d",$startTime),date("Y",$startTime));
			$where .= " and addTime >= $startTime";
		}

		if($endDate){
			$endTime = strtotime($endDate);
			$endTime = mktime(23,59,59,date("m",$endTime),date("d",$endTime),date("Y",$endTime));
			$where .= " and addTime <= $endTime";
		}

		$pointM = M('statistic_point_daily',null,DB_MAIN_CFG);
		$count = $pointM->where($where)->count();
		$this->smarty->assign('totalDataCount',$count);

		$limit = I('get.limit',10,'int');
		$page = new Page($count,$limit);
		fetchPage($this->smarty,$page);

		$data = $pointM
			->where($where)
			->field("addTime,totalPoint,dayOutPutPoint,signOutPutPoint,commentOutPutPoint,praiseOutPutPoint,contributeOutPutPoint,outPutCount")
			->order("addTime DESC")
			->limit($page->firstRow . ',' . $page->listRows)
			->select();
		$this->smarty->assign("obj",$data);
		$total = $pointM
			->where($where)
			->field("sum(dayOutPutPoint) as totalDayAdd,sum(signOutPutPoint) as totalSignAdd,sum(commentOutPutPoint) as totalCommentAdd,sum(praiseOutPutPoint) as totalPraiseAdd,sum(contributeOutPutPoint) as totalContributeAdd")
			->select();
		$this->smarty->assign("total",$total);

		$outPutCount = M('u_point_log',null,DB_MAIN_CFG)->where($where." and addOrReduce=".VAL_ADD)->count("distinct uid");
		$this->smarty->assign("totalCount",$outPutCount);

		$this->smarty->assign('startDate',$startDate);
		$this->smarty->assign('endDate',$endDate);
		$this->smarty->assign('limit',$limit);
		$this->smarty->display("Analysis/pointOutPut.html");
	}
	/**
	 * 积分统计-积分消耗
	 * Author:wwl
	 */
	public function pointConsume(){
		$startDate = I('get.startDate','');
		$endDate = I('get.endDate','');
		$where = "1";

		if($startDate){
			$startTime = strtotime($startDate);
			$startTime = mktime(0,0,0,date("m",$startTime),date("d",$startTime),date("Y",$startTime));
			$where .= " and addTime >= $startTime";
		}
		if($endDate){
			$endTime = strtotime($endDate);
			$endTime = mktime(23,59,59,date("m",$endTime),date("d",$endTime),date("Y",$endTime));
			$where .= " and addTime <= $endTime";
		}
		$pointM = M('statistic_point_daily',null,DB_MAIN_CFG);
		$count = $pointM->where($where)->count();
		$this->smarty->assign('totalDataCount',$count);

		$limit = I('get.limit',10,'int');
		$page = new Page($count,$limit);
		fetchPage($this->smarty,$page);

		$data = $pointM
			->where($where)
			->field("addTime,dayConsumePoint,fullLotteryConsumePoint,fixLotteryConsumePoint,pointMallConsumePoint,applyEventConsumePoint,consumeCount")
			->order("addTime DESC")
			->limit($page->firstRow . ',' . $page->listRows)
			->select();
		$this->smarty->assign("obj",$data);
		$total = $pointM
			->where($where)
			->field("sum(dayConsumePoint) as totalDayMinus,sum(fullLotteryConsumePoint) as totalFullMinus,sum(fixLotteryConsumePoint) as totalFixMinus,sum(pointMallConsumePoint) as totalExchangeMinus,sum(applyEventConsumePoint) as totalEventMinus")
			->select();
		$this->smarty->assign("total",$total);

		$consumeCount = M('u_point_log',null,DB_MAIN_CFG)->where($where." and addOrReduce=".VAL_REDUCE)->count("distinct uid");
		$this->smarty->assign("totalCount",$consumeCount);

		$this->smarty->assign('startDate',$startDate);
		$this->smarty->assign('endDate',$endDate);
		$this->smarty->assign('limit',$limit);
		$this->smarty->display("Analysis/pointOutPut.html");
	}
	/**
	 * 用户日活跃度分析
	 * Author: shijy
	 */
	public function dailyActive(){
		$startDate = I('get.startDate','');
		$endDate = I('get.endDate','');
		$where = "1";

		if($startDate){
			$startTime = strtotime($startDate);
			$startTime = mktime(0,0,0,date("m",$startTime),date("d",$startTime),date("Y",$startTime));
			$where .= " and addTime >= $startTime";
		}

		if($endDate){
			$endTime = strtotime($endDate);
			$endTime = mktime(23,59,59,date("m",$endTime),date("d",$endTime),date("Y",$endTime));
			$where .= " and addTime <= $endTime";
		}

		$dailyActiveM = M('stat_active_daily',null,DB_MAIN_CFG);
		$count = $dailyActiveM->where($where)->count();
		$this->smarty->assign('totalDataCount',$count);

		$limit = I('get.limit',10,'int');
		$page = new Page($count,$limit);
		fetchPage($this->smarty,$page);

		$data = $dailyActiveM->where($where)->order("timeStr DESC")->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->smarty->assign('data',$data);
		$total = $dailyActiveM
			->field("sum(totalActiveUser) as totalActiveUserNum,sum(newUser) as totalNewUserNum,sum(oldUser) as totalOldUserNum,sum(lossComeback) as totalLossComebackNum")
			->where($where)
			->select();
		$this->smarty->assign('total',$total);

		$this->smarty->assign('startDate',$startDate);
		$this->smarty->assign('endDate',$endDate);
		$this->smarty->assign('limit',$limit);
		$this->smarty->display('Analysis/activeDaily.html');
	}
	/**
	 * 用户月活跃度分析
	 */
	public function monthlyActive(){
		$startDate = I('get.startDate','');
		$endDate = I('get.endDate','');
		$where = "1";
		
		if($startDate){
			$startTime = strtotime($startDate);
			$startTime = mktime(0,0,0,date("m",$startTime),date("d",$startTime),date("Y",$startTime));
			$where .= " and addTime >= $startTime";
		}

		if($endDate){
			$endTime = strtotime($endDate);
			$endTime = mktime(23,59,59,date("m",$endTime),date("d",$endTime),date("Y",$endTime));
			$where .= " and addTime <= $endTime";
		}

		$monthlyActiveM = M('stat_active_monthly',null,DB_MAIN_CFG);
		$count = $monthlyActiveM->where($where)->count();
		$this->smarty->assign('totalDataCount',$count);

		$limit = I('get.limit',10,'int');
		$page = new Page($count,$limit);
		fetchPage($this->smarty,$page);

		$data = $monthlyActiveM->where($where)->order("timeStr DESC")->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->smarty->assign('data',$data);
		$total = $monthlyActiveM
			->field("sum(totalActiveUser) as totalActiveUserNum,sum(newUser) as totalNewUserNum,sum(oldUser) as totalOldUserNum,sum(lossComeback) as totalLossComebackNum")
			->where($where)
			->select();
		$this->smarty->assign('total',$total);

		$this->smarty->assign('startDate',$startDate);
		$this->smarty->assign('endDate',$endDate);
		$this->smarty->assign('limit',$limit);
		$this->smarty->display('Analysis/ActiveMonthly.html');
	}
	/**
	 * 活动受众类型分析
	 * Author:wwl
	 */
	public function eventAudience(){
		$event = M('statistic_type_event',null,DB_MAIN_CFG)->order("eventNum DESC")->select();
		$this->smarty->assign('event',$event);
		$this->smarty->display('Analysis/audienceEvent.html');
	}
	/**
	 * 文章受众类型分析
	 * Author:wwl
	 */
	public function articleAudience(){
		$article = M('statistic_type_article',null,DB_MAIN_CFG)->order("articleNum DESC")->select();
		$this->smarty->assign('article',$article);
		$this->smarty->display('Analysis/audienceArticle.html');
	}
	/**
	 * 商品受众类型分析
	 * Author:wwl
	 */
	public function goodsAudience(){
		$goods = M('statistic_type_goods',null,DB_MAIN_CFG)->order("goodsNum DESC")->select();
		$this->smarty->assign('goods',$goods);
		$this->smarty->display('Analysis/audienceGoods.html');
	}

	/**
	 *用户分布分析-地区
	 * Author: wwl
	 */
	public function areaDistribute()
	{
		$data = M('statistic_user_distribute', null, DB_MAIN_CFG)
			->field("area,totalNum,maleNum,femaleNum")
			->order("totalNum DESC")
			->select();
		$totalUserNum = M('u_user_base', null, DB_MAIN_CFG)->count();
		$total = array(
			'totalNum'		=> 0,
			'totalMaleNum'	=> 0,
			'totalFemaleNum'=> 0
		);
		$charDataX = array();
		$charDataY = array();

		foreach ($data as $key => $val) {
			$areaName = explode(',', $val['area']);
			if($areaName[0] <= 0 || !$areaName[0]){
				$data[$key]['areaName'] = "未知";
			} else{
				$path = WEB_ROOT . '/data/area_info/' . $areaName[0] . '.php';
				$kv = include($path);
				$province = $kv[$areaName[0]]['short_name'];

				if($areaName[1] <=0 || !$areaName[1]){
					$data[$key]['areaName'] = $province.".未知";
				} else{
					$city = $kv[$areaName[1]]['short_name'];
					if($areaName[2] <=0 || !$areaName[2]){
						$data[$key]['areaName'] = $province.'.'.$city.".未知";
					} else{
						$district = $kv[$areaName[2]]['short_name'];
						$data[$key]['areaName'] = $province.'.'.$city.'.'.$district;
						//柱状图数据
						$charDataX[] = $district;
						$charDataY[] = $val['totalNum'];
					}
				}
			}
			$data[$key]['totalRatio'] = round($val['totalNum'] / $totalUserNum * 100, 2) . "%";
			$data[$key]['maleRatio'] = round($val['maleNum'] / $totalUserNum * 100, 2) . "%";
			$data[$key]['femaleRatio'] = round($val['femaleNum'] / $totalUserNum * 100, 2) . "%";

			//总计
			$total['totalNum'] += $val['totalNum'];
			$total['totalMaleNum'] += $val['maleNum'];
			$total['totalFemaleNum'] += $val['femaleNum'];
		}

		$this->smarty->assign('xAxis',json_encode($charDataX));
		$this->smarty->assign('yAxis',implode(',',$charDataY));

		$this->smarty->assign('data', $data);
		//总计求百分比
		$total['totalNumRatio'] = round($total['totalNum'] / $totalUserNum * 100, 2).'%';
		$total['totalMaleRatio'] = round($total['totalMaleNum'] / $totalUserNum * 100, 2).'%';
		$total['totalFemaleRatio'] = round($total['totalFemaleNum'] / $totalUserNum * 100, 2).'%';
		$this->smarty->assign('total',$total);

		$this->smarty->display('Analysis/userAreaDistribute.html');
	}

	/**
	 * 用户分布分析-年龄分布
	 * Author:wwl
	 */
	public function ageDistribute(){
		$data = M('statistic_user_age',null,DB_MAIN_CFG)->select();
		$totalUserCount = M('u_user_base',null,DB_MAIN_CFG)->count();
		$total = array(
			'totalNumber'	=> 0,
			'totalMale'		=> 0,
			'totalFemale'	=> 0,
		);
		foreach ($data as $key => $val){
			$data[$key]['groupRatio'] = round($val['groupNum']/$totalUserCount*100,2).'%';
			$data[$key]['maleRatio'] = round($val['maleNum']/$totalUserCount*100,2).'%';
			$data[$key]['femaleRatio'] = round($val['femaleNum']/$totalUserCount*100,2).'%';
			//总计
			$total['totalNumber'] += $val['groupNum'];
			$total['totalMale']	+= $val['maleNum'];
			$total['totalFemale'] += $val['femaleNum'];

			if($val['age'] == '0-0'){
				$data[$key]['age'] = "未知";
			}
		}
		array_multisort($data,SORT_ASC);	//排序
		//总计求概率
		$total['totalRatio'] = round($total['totalNumber']/$totalUserCount*100,2).'%';
		$total['totalMaleRatio'] = round($total['totalMale']/$totalUserCount*100,2).'%';
		$total['totalFemaleRatio'] = round($total['totalFemale']/$totalUserCount*100,2).'%';
		$this->smarty->assign('total',$total);
		$this->smarty->assign("data",$data);
		$this->smarty->display('Analysis/userAgeDistribute.html');
	}

	/**
	 * 统计报表-日报表
	 * Author:wwl
	 */
	public function dayReportForm(){
		$startDate = I('get.startDate','');
		$endDate = I('get.endDate','');
		$where = "1";

		if($startDate){
			$startTime = strtotime($startDate);
			$startTime = mktime(0,0,0,date("m",$startTime),date("d",$startTime),date("Y",$startTime));
			$where .= " and addTime >= $startTime";
		}

		if($endDate){
			$endTime = strtotime($endDate);
			$endTime = mktime(23,59,59,date("m",$endTime),date("d",$endTime),date("Y",$endTime));
			$where .= " and addTime <= $endTime";
		}

		$dayReportM = M('statistic_report_daily',null,DB_MAIN_CFG);
		$count = $dayReportM->where($where)->count();
		$this->smarty->assign('totalDataCount',$count);

		$limit = I('get.limit',10,'int');
		$page = new Page($count,$limit);
		fetchPage($this->smarty,$page);

		$data = $dayReportM->where($where)->order("addTime DESC")->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->smarty->assign('data',$data);
		$total = $dayReportM
			->field("sum(addUserNum) as totalUserNum,sum(addBindNum) as totalBindNum,sum(addOrderNum) as totalOrderNum,sum(praiseNum) as totalPraiseNum,sum(commentNum) as totalCommentNum,sum(writeArticleNum) as totalWriteNum,sum(activeNum) as totalActiveNum,sum(exchangeNum) as totalExchangeNum,sum(cashNum) as totalCash,sum(payNum) as totalPayer")
			->where($where)
			->select();
		$this->smarty->assign('total',$total);

		$this->smarty->assign('startDate',$startDate);
		$this->smarty->assign('endDate',$endDate);
		$this->smarty->assign('limit',$limit);
		$this->smarty->display('Analysis/reportFormDaily.html');
	}
	/**
	 * 统计报表-周报表
	 * Author:wwl
	 */
	public function weekReportForm(){
		$startDate = I('get.startDate','');
		$endDate = I('get.endDate','');
		$where = "1";

		if($startDate){
			$startTime = strtotime($startDate);
			$startTime = mktime(0,0,0,date("m",$startTime),date("d",$startTime),date("Y",$startTime));
			$where .= " and addTime >= $startTime";
		}

		if($endDate){
			$endTime = strtotime($endDate);
			$endTime = mktime(23,59,59,date("m",$endTime),date("d",$endTime),date("Y",$endTime));
			$where .= " and addTime <= $endTime";
		}

		$weekReportM = M('statistic_report_weekly',null,DB_MAIN_CFG);
		$count = $weekReportM->where($where)->count();
		$this->smarty->assign('totalDataCount',$count);

		$limit = I('get.limit',10,'int');
		$page = new Page($count,$limit);
		fetchPage($this->smarty,$page);

		$data = $weekReportM->where($where)->order("addTime DESC")->limit($page->firstRow . ',' . $page->listRows)->select();
		foreach ($data as $key => $val){
			$data[$key]['weekBeginTime'] = mktime(0,0,0,date('m',$val['addTime']),date('d',$val['addTime'])-date('w',$val['addTime'])+1,date('Y',$val['addTime']));
			$data[$key]['weekEndTime'] = mktime(23,59,59,date('m',$val['addTime']),date('d',$val['addTime'])-date('w',$val['addTime'])+7,date('Y',$val['addTime']));
		}
		$this->smarty->assign('data',$data);

		$this->smarty->assign('startDate',$startDate);
		$this->smarty->assign('endDate',$endDate);
		$this->smarty->assign('limit',$limit);
		$this->smarty->display('Analysis/reportFormWeekly.html');
	}
	/**
	 * 统计报表-月报表
	 * Author:wwl
	 */
	public function monReportForm(){
		$startDate = I('get.startDate','');
		$endDate = I('get.endDate','');
		$where = "1";

		if($startDate){
			$startTime = strtotime($startDate);
			$startTime = mktime(0,0,0,date("m",$startTime),date("d",$startTime),date("Y",$startTime));
			$where .= " and addTime >= $startTime";
		}

		if($endDate){
			$endTime = strtotime($endDate);
			$endTime = mktime(23,59,59,date("m",$endTime),date("d",$endTime),date("Y",$endTime));
			$where .= " and addTime <= $endTime";
		}

		$weekReportM = M('statistic_report_monthly',null,DB_MAIN_CFG);
		$count = $weekReportM->where($where)->count();
		$this->smarty->assign('totalDataCount',$count);

		$limit = I('get.limit',10,'int');
		$page = new Page($count,$limit);
		fetchPage($this->smarty,$page);

		$data = $weekReportM->where($where)->order("addTime DESC")->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->smarty->assign('data',$data);

		$this->smarty->assign('startDate',$startDate);
		$this->smarty->assign('endDate',$endDate);
		$this->smarty->assign('limit',$limit);
		$this->smarty->display('Analysis/reportFormMonthly.html');
	}

	/**
	 * 留存分析
	 * Author: wwl
	 */
	public function remain(){
		$startDate = I('get.startDate','');
		$endDate = I('get.endDate','');
		$where = "1";

		if($startDate){
			$startTime = strtotime($startDate);
			$startTime = mktime(0,0,0,date("m",$startTime),date("d",$startTime),date("Y",$startTime));
			$where .= " and addTime >= $startTime";
		}

		if($endDate){
			$endTime = strtotime($endDate);
			$endTime = mktime(23,59,59,date("m",$endTime),date("d",$endTime),date("Y",$endTime));
			$where .= " and addTime <= $endTime";
		}

		$remainM = M('statistic_user_remain',null,DB_MAIN_CFG);
		$count = $remainM->where($where)->count();
		$this->smarty->assign('totalDataCount',$count);

		$limit = I('get.limit',10,'int');
		$page = new Page($count,$limit);
		fetchPage($this->smarty,$page);

		$data = $remainM->where($where)->order("objTime DESC")->limit($page->firstRow . ',' . $page->listRows)->select();
		foreach ($data as $k => $v){
			$data[$k]['nextRemainRatio'] = $data[$k]['thirdRemainRatio'] = $data[$k]['seventhRemainRatio']= $data[$k]['fifteenthRemainRatio'] = $data[$k]['thirtiethRemainRatio']= "0%";
			if($v['newUserCount'] > 0){
				$data[$k]['nextRemainRatio'] = round($v['nextDayRemain']/$v['newUserCount']*100,2)."%";
				$data[$k]['thirdRemainRatio'] = round($v['thirdDayRemain']/$v['newUserCount']*100,2)."%";
				$data[$k]['seventhRemainRatio'] = round($v['seventhDayRemain']/$v['newUserCount']*100,2)."%";
				$data[$k]['fifteenthRemainRatio'] = round($v['fifteenthDayRemain']/$v['newUserCount']*100,2)."%";
				$data[$k]['thirtiethRemainRatio'] = round($v['thirtiethDayRemain']/$v['newUserCount']*100,2)."%";
			}
		}
		$this->smarty->assign('data',$data);

		$total =  $remainM
			->where($where)
			->field("sum(newUserCount) as totalNumber,sum(nextDayRemain) as nextRemainCount,sum(thirdDayRemain) as thirdRemainCount,sum(seventhDayRemain) as seventhRemainCount,sum(fifteenthDayRemain) as fifteenthRemainCount,sum(thirtiethDayRemain) as thirtiethRemainCount")
			->select();
		foreach ($total as $k => $v){
			$total[$k]['nextRemainRatio'] = $total[$k]['thirdRemainRatio'] = $total[$k]['seventhRemainRatio']= $total[$k]['fifteenthRemainRatio'] = $total[$k]['thirtiethRemainRatio']= "0%";
			if($v['newUserCount'] > 0){
				$total[$k]['nextRemainRatio'] = round($v['nextDayRemain']/$v['newUserCount']*100,2)."%";
				$total[$k]['thirdRemainRatio'] = round($v['thirdDayRemain']/$v['newUserCount']*100,2)."%";
				$total[$k]['seventhRemainRatio'] = round($v['seventhDayRemain']/$v['newUserCount']*100,2)."%";
				$total[$k]['fifteenthRemainRatio'] = round($v['fifteenthDayRemain']/$v['newUserCount']*100,2)."%";
				$total[$k]['thirtiethRemainRatio'] = round($v['thirtiethDayRemain']/$v['newUserCount']*100,2)."%";
			}
		}
		$this->smarty->assign('total',$total);

		$this->smarty->assign('startDate',$startDate);
		$this->smarty->assign('endDate',$endDate);
		$this->smarty->assign('limit',$limit);
		$this->smarty->display('Analysis/userRemain.html');
	}

	/**
	 * 用户喜好分析
	 * Author: wwl
	 */
	public function userLike(){
		$keywords = I('get.keywords',null);
		$ofield=I('get.ofield');
		$otype=I('get.otype','');
		if(strcmp($otype,'')==0) {
			$field=null;
		}else{
			$field = "index$ofield";
		}

		if($field){
			$order="{$field} {$otype}";
		}else{
			$order="uid ASC";
		}
		$where = "1";
		if($keywords){
			$where .= " and (nickname like '%{$keywords}%' OR realName like '%{$keywords}%')";
		}
		$likeM =  M('statistic_user_like',null,DB_MAIN_CFG);
		$count = $likeM->where($where)->count();
		$this->smarty->assign('totalDataCount',$count);

		$limit = I('get.limit',10,'int');
		$page = new Page($count,$limit);
		fetchPage($this->smarty,$page);

		$data = $likeM->where($where)->order($order)->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->smarty->assign("data",$data);

		$index = getContentTypeKv();
		$this->smarty->assign("index",$index);

		$this->smarty->assign('ofield',$ofield);
		$this->smarty->assign('otype',$otype);
		$this->smarty->assign('limit',$limit);
		$this->smarty->assign("keywords",$keywords);
		$this->smarty->display('Analysis/userLike.html');
	}

	/**
	 * 用户喜好分析-指数统计
	 * Author:wwl
	 */
	public function indexAnalysis(){
		$index = getContentTypeKv();
		$this->smarty->assign("index",$index);

		$likeM = M("statistic_user_like",null,DB_MAIN_CFG);
		$data = array(); $i = 0;
		foreach ($index as $k => $v){
			$i++;
			$data[$i]['indexID'] = $k;
			$data[$i]['indexName'] = $v;
			$data[$i]["joinNumber"] = $likeM->where("index$k > 0")->count("distinct uid");
		}

		$field="count(1) as totalNumber";
		foreach ($index as $key => $val){
			$field .= ", sum(index$key) as totalIndex$key";
		}
		$totalIndex = $likeM->field($field)->select();

		$joinNumber = array();$contentType = array();
		foreach ($data as $key => $val){
			foreach ($totalIndex as $item) {
				if($val['indexID']){
					$k = $val['indexID'];
					$data[$key]['totalIndexNum'] = $item["totalIndex$k"];
				}
				$data[$key]['joinNumberRatio'] = round($val['joinNumber']/$item['totalNumber']*100,1).'%';
			}

			if($data[$key]['totalIndexNum'] <= 0){
				unset($data[$key]);
			} else{
				$joinNumber[] = $val['joinNumber'];
				$contentType[] = $val['indexName'];
			}
		}

		$this->smarty->assign('xAxis',json_encode($contentType));
		$this->smarty->assign('yAxis',implode(',',$joinNumber));

		$data = arraySequence($data,'totalIndexNum',SORT_DESC);

		$this->smarty->assign("data",$data);
		$this->smarty->display('Analysis/userIndexStatistic.html');
		
	}

	/**
	 * 用户流失分析
	 * Author:wwl
	 */
	public function userLose(){
		$startDate = I('get.startDate','');
		$endDate = I('get.endDate','');
		$where = "1";

		if($startDate){
			$startTime = strtotime($startDate);
			$startTime = mktime(0,0,0,date("m",$startTime),date("d",$startTime),date("Y",$startTime));
			$where .= " and addTime >= $startTime";
		}

		if($endDate){
			$endTime = strtotime($endDate);
			$endTime = mktime(23,59,59,date("m",$endTime),date("d",$endTime),date("Y",$endTime));
			$where .= " and addTime <= $endTime";
		}

		$lostM = M("statistic_user_lost",null,DB_MAIN_CFG);
		$count = $lostM->where($where)->count();
		$this->smarty->assign('totalDataCount',$count);

		$limit = I('get.limit',10,'int');
		$page = new Page($count,$limit);
		fetchPage($this->smarty,$page);

		$data = $lostM->where($where)->order("addTime DESC")->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->smarty->assign("data",$data);

		$this->smarty->assign('startDate',$startDate);
		$this->smarty->assign('endDate',$endDate);
		$this->smarty->assign('limit',$limit);
		$this->smarty->display('Analysis/userLose.html');
	}
	/**
	 * 流失用户来源统计
	 * Author:wwl
	 */
	public function clientFromLost(){
		$data = M('statistic_user_from',null,DB_MAIN_CFG)->select();
		$clientFrom = C("CLIENT_FROM");
		foreach ($data as $key => $val){
			if($val['clientFrom'] == 0){    //全部类去除
                unset($data[$key]);
                continue;
            }
			foreach ($clientFrom as $k => $item){
				if($val['clientFrom'] == $k){
					$data[$key]['clientName'] = $item;
				}
			}
		}
		$this->smarty->assign('data',$data);
		$this->smarty->display("Analysis/userFromLost.html");
	}

	/**
	 * 用户类型分析
	 * Author: wwl
	 */
	public function userType(){
		$startDate = I('get.startDate','');
		$endDate = I('get.endDate','');
		$where = "1";

		if($startDate){
			$startTime = strtotime($startDate);
			$startTime = mktime(0,0,0,date("m",$startTime),date("d",$startTime),date("Y",$startTime));
			$where .= " and addTime >= $startTime";
		}

		if($endDate){
			$endTime = strtotime($endDate);
			$endTime = mktime(23,59,59,date("m",$endTime),date("d",$endTime),date("Y",$endTime));
			$where .= " and addTime <= $endTime";
		}

		$typeM = M('statistic_user_type',null,DB_MAIN_CFG);
		$count = $typeM->where($where)->count();
		$this->smarty->assign('totalDataCount',$count);

		$limit = I('get.limit',10,'int');
		$page = new Page($count,$limit);
		fetchPage($this->smarty,$page);

		$data = $typeM->where($where)->order("addTime DESC")->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->smarty->assign("obj",$data);

		$this->smarty->assign('startDate',$startDate);
		$this->smarty->assign('endDate',$endDate);
		$this->smarty->assign('limit',$limit);
		$this->smarty->display('Analysis/userType.html');
	}
	/**
	 * 用户统计-在线时间分布
	 * Author:wwl
	 */
	public function onlineTimeDistribute(){
		//获取上周起始时间戳和结束时间戳
		$beginLastWeek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
		$endLastWeek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));

		$data = M('statistic_user_online',null,DB_MAIN_CFG)->field("round(avg(groupNumber)) as groupNumber,week,timeGroup")->group("week,timeGroup")->select();
		$this->smarty->assign("data",$data);

		$total = array('MonNumber'=>0,'TueNumber'=>0,'WedNumber'=>0,'ThuNumber'=>0,'FriNumber'=>0,'SatNumber'=>0,'SunNumber'=>0);
		foreach ($data as $val){
			if($val['week'] == 0){		//周日
				$total['SunNumber'] += $val['groupNumber'];
			} elseif ($val['week'] == 1){
				$total['MonNumber'] += $val['groupNumber'];
			} elseif ($val['week'] == 2){
				$total['TueNumber'] += $val['groupNumber'];
			} elseif ($val['week'] == 3){
				$total['WedNumber'] += $val['groupNumber'];
			} elseif ($val['week'] == 4){
				$total['ThuNumber'] += $val['groupNumber'];
			} elseif ($val['week'] == 5){
				$total['FriNumber'] += $val['groupNumber'];
			} elseif ($val['week'] == 6){
				$total['SatNumber'] += $val['groupNumber'];
			}
		}
		$this->smarty->assign('total',$total);
		$this->smarty->display("Analysis/userOnlineDistribute.html");
	}


}
